package bulletGraph
{
	import mx.core.UIComponent;
	import flash.geom.Point;
	import flash.display.Sprite;
	import colorpicker.ColorUtil;
	
	
	[Style(name="margin", type="Number")]
	public class BulletGraphPlotArea extends UIComponent
	{
		
		private static var defaultStyles:Object = 
		{
			margin: 20,
			fillColor: 0x92d050,
			darkness: [0.40, 0.25, 0.10],
			comparativeFillColor:0x00b050,
			performanceFillColor:0x0070c0,
			/*
			0x0070c0,
		 	0x002060,
		 	0x7030a0*/
			majorUnit:50,
			majorTickColor:0x7030a0,
			majorTickThickness:1,
			majorTickLength:6
		};
		
		private static var myClassName:String = "BulletGraphPlotArea";
		include "../charts/core/defaultStyles.as";

	//--- children
		private var _performanceSprite:Sprite;
		private var _comparativeSprite:Sprite;
		private var _tickLables:Array;
		

		private var _scales:Array = [200,250];
		private var _minScaleValue:Number = 0;
		private var _maxScaleValue:Number = 300;
		
		private var _comparativeValue:Number = 260;
		private var _performanceValue:Number = 275;		
		
		override protected function createChildren():void
		{
			super.createChildren();
			if(!_comparativeSprite)
			{
				_comparativeSprite = new Sprite();
				this.addChild(_comparativeSprite);
			}
			if(!_performanceSprite)
			{
				_performanceSprite = new Sprite();
				this.addChild(_performanceSprite);
			}
		}
		
		override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
		{
			super.updateDisplayList(unscaledWidth, unscaledHeight);
			
			var margin:Number = this.getStyle("margin");
			
			var points:Array = [];
			points.push(new Point(0,margin));
			for(var i:int=0;i<_scales.length;i++)
			{
				points.push(new Point( (_scales[i]/_maxScaleValue)*unscaledWidth,margin));
			}
			points.push(new Point(unscaledWidth,margin));
			
			var fillColors:Array = computeFillColors();
			// draw fill rectangle
			this.graphics.clear();
			var height:Number = unscaledHeight - margin*2;
			for(var j:int=0;j<points.length-1;j++)
			{
				this.graphics.beginFill(fillColors[j%fillColors.length],1.0);
				var width:Number = points[j+1].x-points[j].x;
				this.graphics.drawRect(points[j].x,points[j].y,width,height);
			}
			this.graphics.endFill();
			
			
			// draw tick
			var majorTickColor:uint = getStyle("majorTickColor");
			var majorUnit:Number = getStyle("majorUnit");
			var majorTickThickness:uint = getStyle("majorTickThickness");
			var majorTickLength:Number = getStyle("majorTickLength");
			
			var yValue:Number = unscaledHeight - margin;
			var xValuePiece:Number = (majorUnit/_maxScaleValue) * unscaledWidth;
			var xValue:Number = 0;
			this.graphics.lineStyle(majorTickThickness,majorTickColor);
			for(i=0;i<_maxScaleValue/majorUnit+1;i++)
			{
				xValue = xValuePiece*i;
				this.graphics.moveTo(xValue,yValue);
				this.graphics.lineTo(xValue,yValue+majorTickLength);
			}
			
			// draw performance bar & comparative
			var performanceFillColor:uint = this.getStyle("performanceFillColor");
			if(_performanceSprite)
			{
				_performanceSprite.graphics.beginFill(performanceFillColor,1.0);
				_performanceSprite.graphics.drawRect(0,0,_performanceValue/_maxScaleValue * unscaledWidth,height/3);
				_performanceSprite.graphics.endFill();
				_performanceSprite.y = margin+height/3;
			}
			
			var comparativeFillColor:uint = this.getStyle("comparativeFillColor");
			var comparativeWidth:Number = 2;
			if(_comparativeSprite)
			{
				_comparativeSprite.graphics.beginFill(comparativeFillColor,1.0);
				_comparativeSprite.graphics.drawRect(0,0, comparativeWidth, height/6 *4);
				_comparativeSprite.graphics.endFill();
				_comparativeSprite.x = _comparativeValue/_maxScaleValue * unscaledWidth - comparativeWidth/2;
				_comparativeSprite.y = margin+height/6;
			}
			
		}
		private function computeFillColors():Array
		{
			var fillColor:uint = this.getStyle("fillColor");
			var darkness:Array = this.getStyle("darkness");
			
			var HSL:Array = ColorUtil.Num2HSL( fillColor );
			var L:uint = HSL[2];
			
			var percentage:Number;
			
			var fillColors:Array = [];
			for(var i:int = 0;i<darkness.length;i++){
				var newL:uint = (1 - darkness[i]) * L;
				var newColor:uint = ColorUtil.HSL2Num( HSL[0], HSL[1], newL );
				fillColors.push(newColor);
			}
			return fillColors;
		}
		
	}
}